﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XModem.library
{
   public static class SYSHelper
    {


       

        /// <summary>
        /// 计算波特率
        /// </summary>
        /// <param name="sys_freq">波特率</param>
        /// <returns></returns>
        public static List<string> GetFSys(double sys_freq,int digit) {
            List<string> list = new List<string>();
            //int sysNum =(int) Math.Round(((sys_freq*1000.0) * 1024 ) / (36),0);
            int sysNum = (int)Math.Round((sys_freq * 1000000), 0);
            List<char> sysStr = new List<char>();
            if (digit == 4)
            {
                sysStr = sysNum.ToString("X4").ToList();
            }
            if (digit == 8)
            {
                sysStr = sysNum.ToString("X8").ToList();
            }
            for (int i = 0; i < sysStr.Count; )
            {
                list.Add(string.Format("{0}{1}", sysStr[i], sysStr[i+1]));
                i += 2;
            }
            return list;
        }
        /// <summary>
        /// int类型转uint转list集合
        /// </summary>
        /// <param name="uintToString">uint的int类型值</param>
        /// <param name="digit">4位和8位</param>
        /// <returns></returns>
        public static List<string> GetUintToList(int uintToString, int digit) {
            List<string> list = new List<string>();
            List<char> sysStr = new List<char>();
            if (digit == 4)
            {
                sysStr = uintToString.ToString("X4").ToList();
            }
            if (digit == 8)
            {
                sysStr = uintToString.ToString("X8").ToList();
            }

            for (int i = 0; i < sysStr.Count;)
            {
                list.Add(string.Format("{0}{1}", sysStr[i], sysStr[i + 1]));
                i += 2;
            }
            return list;
        }

        /// <summary>
        /// 计算波特率
        /// </summary>
        /// <param name="bpsNum">波特率</param>
        /// <param name="sys_freq">系统频率</param>
        /// <param name="digit">长度</param>
        /// <returns></returns>
        public static List<string> GetFsys_Bps(double bpsNum,double sys_freq, int digit) {

            List<string> list = new List<string>();
            int sysNum = (int)Math.Round((sys_freq * 1000000.0) / (bpsNum), 0) -1;
            List<char> sysStr = new List<char>();
            if (digit == 4)
            {
                sysStr = sysNum.ToString("X4").ToList();
            }
            if (digit == 8)
            {
                sysStr = sysNum.ToString("X8").ToList();
            }
            for (int i = 0; i < sysStr.Count;)
            {
                list.Add(string.Format("{0}{1}", sysStr[i], sysStr[i + 1]));
                i += 2;
            }
            return list;
        }
        /// <summary>
        /// 修改系统频率
        /// </summary>
        /// <param name="list"></param>
       public  static void SetFsysBps(List<string> list)
        {
            ParaList.SysParaList.ForEach(c =>
            {

                switch (c.PKey)
                {
                    case "0x004c":
                        c.PValue =string.Format("0x{0}", list[3]) ;
                        break;
                    case "0x004d":
                        c.PValue = string.Format("0x{0}", list[2]);
                        break;
                    case "0x004e":
                        c.PValue = string.Format("0x{0}", list[1]);
                        break;
                    case "0x004f":
                        c.PValue = string.Format("0x{0}", list[0]);
                        break;
                    default:
                        break;
                }

            });
        }
        /// <summary>
        /// 修改比特率
        /// </summary>
        /// <param name="list"></param>
        public static void SetFsysSystem(List<string> list)
        {
            ParaList.SysParaList.ForEach(c =>
            {

                switch (c.PKey)
                {
                    //case "0x400b":
                    case "0x500a":
                    case "0x500b":
                        c.PValue = string.Format("0x{0}", list[0]); 
                        break;
                    //case "0x4004":
                    case "0x5003":
                    case "0x5004":
                        c.PValue = string.Format("0x{0}", list[1]);
                        break;
                    default:
                        break;
                }

            });
        }
        /// <summary>
        /// 发送端数据长度
        /// </summary>
        /// <param name="list"></param>
        /// <param name="listpara"></param>
        public static void SetTxFlashLength(List<string> list,List<ParaModel> listpara) {

            listpara.ForEach(c =>
            {

                switch (c.PKey)
                {
                    case "0x1330":
                        c.PValue = string.Format("0x{0}", list[3]);
                        break;
                    case "0x1331":
                        c.PValue = string.Format("0x{0}", list[2]);
                        break;
                    case "0x1332":
                        c.PValue = string.Format("0x{0}", list[1]);
                        break;
                    case "0x1333":
                        c.PValue = string.Format("0x{0}", list[0]); 
                        break;
                    default:
                        break;
                }

            });
        }

        public static void SetTxFlashEeprom(List<ParaModel> listpara, bool isxmodel,bool iseeprom) {
            if (isxmodel) //xmodem cc
            {
                //Xmodem_Direction
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1325")
                    {
                        c.PValue = string.Format("0x01");
                    }
                });
                //Xmodem_Mode_Type
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1324")
                    {
                        c.PValue = string.Format("0x00");
                    }
                });


                if (iseeprom)
                {
                    //Xmodem_TX_Data_Src_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132a")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x61");
                        }
                    });
                    //Xmodem_TX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132b")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc2");
                        }
                    });
                }
                else {
                    //Xmodem_TX_Data_Src_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132a")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x21");
                        }
                    });
                    //Xmodem_TX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132b")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc2");
                        }
                    });
                }
               
            }
            else
            {// 1kxmodem cd
             //Xmodem_Direction
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1325")
                    {
                        c.PValue = string.Format("0x01");
                    }
                });
                //Xmodem_Mode_Type
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1324")
                    {
                        c.PValue = string.Format("0x01");
                    }
                });


                if (iseeprom)
                {
                    //Xmodem_TX_Data_Src_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132a")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x61");
                        }
                    });
                    //Xmodem_TX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132b")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc2");
                        }
                    });
                }
                else {
                    //Xmodem_TX_Data_Src_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132a")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x21");
                        }
                    });
                    //Xmodem_TX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x132b")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc2");
                        }
                    });
                }
                
            }
        }
        /// <summary>
        /// 接收端数据长度
        /// </summary>
        /// <param name="list"></param>
        /// <param name="listpara"></param>
        public static void SetRxFlashLength(List<string> list, List<ParaModel> listpara) {
            listpara.ForEach(c =>
            {

                switch (c.PKey)
                {
                    case "0x132c":
                        c.PValue = string.Format("0x{0}", list[3]); 
                        break;
                    case "0x132d":
                        c.PValue = string.Format("0x{0}", list[2]); 
                        break;
                    case "0x132e":
                        c.PValue = string.Format("0x{0}", list[1]); 
                        break;
                    case "0x132f":
                        c.PValue = string.Format("0x{0}", list[0]); 
                        break;
                    default:
                        break;
                }

            });
        }

        public static void SetRxFlashEeprom(List<ParaModel> listpara,bool isxmodel,bool iseeprom) {
            if (isxmodel) //xmodem ca
            {
                //Xmodem_Direction
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1325")
                    {
                        c.PValue = string.Format("0x00");
                    }
                });
                //Xmodem_Mode_Type
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1324")
                    {
                        c.PValue = string.Format("0x00");
                    }
                });
                if (iseeprom)
                {
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x61");
                        }
                    });
                    //Xmodem_RX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x1329")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                   
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc1");
                        }
                    });
                }
                else {
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x21");
                        }
                    });
                    //Xmodem_RX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x1329")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });

                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc1");
                        }
                    });
                }
               
            }
            else {// 1kxmodem cb
                //Xmodem_Direction
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1325")
                    {
                        c.PValue = string.Format("0x00");
                    }
                });
                //Xmodem_Mode_Type
                listpara.ForEach(c =>
                {
                    if (c.PKey == "0x1324")
                    {
                        c.PValue = string.Format("0x01");
                    }
                });
                if (iseeprom)
                {
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x61");
                        }
                    });
                    //Xmodem_RX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x1329")
                        {
                            c.PValue = string.Format("0x01");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc1");
                        }
                    });
                }
                else {
                    ParaList.SysParaList.ForEach(c =>
                    {
                        if (c.PKey == "0x00bc")
                        {
                            c.PValue = string.Format("0x21");
                        }
                    });
                    //Xmodem_RX_Data_Dest_Type
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x1329")
                        {
                            c.PValue = string.Format("0x00");
                        }
                    });
                    listpara.ForEach(c =>
                    {
                        if (c.PKey == "0x0137")
                        {
                            c.PValue = string.Format("0xc1");
                        }
                    });
                }
                
            }
            
        }


        /// <summary>
        /// 生成系统频率字符串
        /// </summary>
        /// <returns></returns>
        public static string CreateFsys() {
            StringBuilder sb = new StringBuilder();
            //sb.Append("[paralist]");
            ParaList.SysParaList.ForEach(c => {
                sb.Append(string.Format("{0},{1}\n",c.PKey,c.PValue));
            });

            return sb.ToString();
        }

        /// <summary>
        /// 创建xmdoel 发送文本
        /// </summary>
        /// <returns></returns>
        public static string CreateTxFlash() {
            StringBuilder sb = new StringBuilder();
            //sb.Append("[paralist]");
            ParaList.Tx_flash.ForEach(c => {
                sb.Append(string.Format("{0},{1}\n", c.PKey, c.PValue));
            });

            return sb.ToString();
        }
        /// <summary>
        /// 创建xmdoel 发送1k文本
        /// </summary>
        /// <returns></returns>
        public static string CreateTxFlash1K() {
            StringBuilder sb = new StringBuilder();
            //sb.Append("[paralist]");
            ParaList.Tx_flash_1k.ForEach(c => {
                sb.Append(string.Format("{0},{1}\n", c.PKey, c.PValue));
            });

            return sb.ToString();
        }



        /// <summary>
        /// 创建xmdoel 接收文本
        /// </summary>
        /// <returns></returns>
        public static string CreateRxFlash()
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append("[paralist]");
            ParaList.Rx_flash.ForEach(c => {
                sb.Append(string.Format("{0},{1}\n", c.PKey, c.PValue));
            });

            return sb.ToString();
        }
        /// <summary>
        /// 创建xmdoel 接收1k文本
        /// </summary>
        /// <returns></returns>
        public static string CreateRxFlash1K()
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append("[paralist]");
            ParaList.Rx_flash_1k.ForEach(c => {
                sb.Append(string.Format("{0},{1}\n", c.PKey, c.PValue));
            });

            return sb.ToString();
        }

        /// <summary>
        /// 获取bin文件长度
        /// </summary>
        /// <param name="file_path"></param>
        /// <returns></returns>
        public static int BinReader(string file_path) {
            int read_len;
            FileStream Myfile = new FileStream(file_path, FileMode.Open, FileAccess.Read);
            BinaryReader binreader = new BinaryReader(Myfile);
            read_len = (int)Myfile.Length;
            binreader.Close();
            Myfile.Close();
            return read_len;
        }

    }
}
